package net.narusas.aceauction.push;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;

@Data
@Slf4j
@EqualsAndHashCode(callSuper = false)
public class SchedulePushService extends TimerTask {
	int min;
	Timer timer;
	AuctionPushService auctionPushService;

	public void enable(boolean enable) {
		if (enable) {
			enableScheduler();
		} else {
			disableScheduler();
		}
	}

	private void enableScheduler() {
		log.info("스케줄 서비스를 활성화합니다. (주기 {})", min);
		if (timer != null) {
			timer.cancel();
		}
		timer = new Timer();
		timer.schedule(this, new Date(), 1000L * 10 * min);
	}

	private void disableScheduler() {
		log.info("스케줄 서비스를 비활성화 합니다.");
		if (timer != null) {
			timer.cancel();
		}
		timer = null;
	}

	@Override
	public void run() {
		log.info("작업 시작시간입니다. {}",  new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
		auctionPushService.pushMessage();
		log.info("작업 종료되었습니다. {}",  new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
	}
}
